package kubeutils

import (
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

// KubeUtilser接口是操作k8s资源的接口
// 需要跟k8s接口打交道，就是使用是接口
type KubeUtilser interface {
	// namespace
	Create(namespace string) error
	// namespace
	Update(namespace string) error

	// namespace, name, gracePeriodSeconds
	Delete(namespace string, name string, gracePeriodSecods *int64) error
	//
	DeleteList(namespace string, namelist []string, gracePeriodSecods *int64) error

	// namespace, labelSelector, fieldSelector
	List(namespace string, labelSelector string, fieldSelector string) (interface{}, error)
	// namespace, name
	Get(namespace string, name string) (interface{}, error)
}

type ResourceInstance struct {
	Kubeconfig string
	Clientset  *kubernetes.Clientset
}

func (r *ResourceInstance) Init(kubeconfig string) {
	r.Kubeconfig = kubeconfig

	restConfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig))
	if err != nil {
		msg := "kubeconfig解析失败" + err.Error()
		panic(msg)
	}
	clientset, err := kubernetes.NewForConfig(restConfig)
	if err != nil {
		msg := "clientset创建失败" + err.Error()
		panic(msg)
	}
	r.Clientset = clientset
}
